package com.tao.drop.dao.impl;

import com.tao.drop.domain.People;
import com.tao.drop.dao.DropMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

/**
 * Created by tao on 2017/8/23.
 */
public class DropMapperImp implements DropMapper {

    //配置SqlSession方法。通用

    public SqlSession sessionConfigure () {
        //mybatis的配置文件*********************
        String resource = "MyBatis-Configuration.xml";
        //使用类加载器加载mybatis的配置文件（它也加载关联的映射文件）
        InputStream is = DropMapperImp.class.getClassLoader().getResourceAsStream(resource);
        //构建sqlSession的工厂
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        //使用MyBatis提供的Resources类加载mybatis的配置文件（它也加载关联的映射文件）
        //Reader reader = Resources.getResourceAsReader(resource);
        //构建sqlSession的工厂
        //SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
        //创建能执行映射文件中sql的sqlSession
        SqlSession session = sessionFactory.openSession();
        return session;
    }


    @Override
    public List<People> selectAll(@Param(value = "age") int age) {

        SqlSession session = this.sessionConfigure();

        //Mapper.xml中对应的sql语句id ********************
        String statement = "Mapper.selectAllSql";
        //******************传入参数
        List<People> peoples = session.selectList(statement,age);
        return peoples;
    }
}
